package cn.wolfcode.wolf2w.destination.service.impl;

import cn.wolfcode.wolf2w.destination.entity.TaDestination;
import cn.wolfcode.wolf2w.destination.entity.TaRegion;
import cn.wolfcode.wolf2w.destination.mapper.TaDestinationMapper;
import cn.wolfcode.wolf2w.destination.mapper.TaRegionMapper;
import cn.wolfcode.wolf2w.destination.query.DestinationQuery;
import cn.wolfcode.wolf2w.destination.service.ITaDestinationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 目的地表 服务实现类
 * </p>
 *
 * @author libai
 * @since 2024-07-23
 */
@Service
public class TaDestinationServiceImpl extends ServiceImpl<TaDestinationMapper, TaDestination> implements ITaDestinationService {
    @Autowired
    private TaDestinationMapper destinationMapper;
    @Autowired
    private TaRegionMapper regionMapper;

    @Override
    public List<TaDestination> search(Long regionId) {
        List<TaDestination> destinationsList=null;
        if (regionId == -1) {
            //国内
            LambdaQueryWrapper<TaDestination> qw = new LambdaQueryWrapper<>();
            qw.eq(TaDestination::getParentId,1);
//            lambdaQuery().eq(TaDestination::getParentId,1).list();
            destinationsList   = destinationMapper.selectList(qw);
        }else {
//            LambdaQueryWrapper<TaRegion> qw = new LambdaQueryWrapper<>();
            TaRegion region = regionMapper.selectById(regionId);
            String refIds = region.getRefIds();
            String[] idStrArr = refIds.split(",");
            List<Long> ids = new ArrayList<>();
            for (String idStr : idStrArr) {
               Long id =Long.valueOf(idStr);
               ids.add(id);
            }
            LambdaQueryWrapper<TaDestination> qw = new LambdaQueryWrapper<>();
            qw.in(TaDestination::getId,ids);
            destinationsList = destinationMapper.selectList(qw);
        }
        for (TaDestination dest :destinationsList){
            Long id = dest.getId();
            List<TaDestination> children = lambdaQuery().eq(TaDestination::getParentId, id).last("limit 10").list();
            dest.setChildren(children);
        }
        return destinationsList;
    }

    @Override
    public List<TaDestination> queryToastsByDestId(Long destId) {
       List<TaDestination> list = new ArrayList<>();
       creatToasts(destId, list);
       //反向排序
//        Collections.reverse(list);
        return list;
    }



    //用递归的方法,查广州父级以上的地点
    private void creatToasts(Long destId,List<TaDestination> list){
        if (destId == null){
            return;
        }
        TaDestination destination = destinationMapper.selectById(destId);
      //回溯
        creatToasts(destination.getParentId(),list);
        list.add(destination);
    }

    @Override
    public List<TaDestination> listAll(DestinationQuery qo) {
        List<TaDestination> list ;
        LambdaQueryWrapper<TaDestination> qw = new LambdaQueryWrapper<>();
        if (qo.getType() == -1){
            //国内
            qw.eq(TaDestination::getParentId,1);
        }else {
            //国外 查询region表
            List<TaRegion> regions = regionMapper.selectList(null);
            List<Long> ids = new ArrayList<>();
            for (TaRegion region : regions) {
                String[] split = region.getRefIds().split(",");
                for (String s : split) {
                    ids.add(Long.valueOf(s));

                }
            }
            qw.in(TaDestination::getId,ids);
        }
        list = destinationMapper.selectList(qw); //一级
        //查二级
        for (TaDestination destination : list) {
            destination.setChildren(Collections.emptyList());
        }
        return list;
    }
}
